package service

import (
	"chat_srv/basic/config"
	chat "chat_srv/basic/proto"
	"chat_srv/handler/dao"
	"context"
)

// 获取随机问题
func (s *Server) GetRandomQuestion(_ context.Context, in *chat.GetRandomQuestionReq) (*chat.GetRandomQuestionResp, error) {
	// 业务逻辑
	resp, err := dao.GetRandomQuestion(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.GetRandomQuestionResp{
		Id:       int64(resp.Id),
		Question: resp.Question,
		Category: resp.Category,
	}, nil
}

// 按分类/兴趣获取随机问题
func (s *Server) GetRandomQuestionByCategory(_ context.Context, in *chat.GetRandomQuestionByCategoryReq) (*chat.GetRandomQuestionByCategoryResp, error) {
	// 业务逻辑
	resp, err := dao.GetRandomQuestionByCategory(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.GetRandomQuestionByCategoryResp{
		Id:       int64(resp.Id),
		Question: resp.Question,
		Category: resp.Category,
	}, nil
}

// 表情包推荐
func (s *Server) GetRandomStickerByCategory(_ context.Context, in *chat.GetRandomStickerByCategoryReq) (*chat.GetRandomStickerByCategoryResp, error) {
	// 业务逻辑
	resp, err := dao.GetRandomStickerByCategory(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	// 转换为pb消息
	var stickers []*chat.RandomSticker
	for _, sticker := range resp {
		stickers = append(stickers, &chat.RandomSticker{
			Id:         int64(sticker.Id),
			Name:       sticker.Name,
			Url:        sticker.Url,
			Category:   sticker.Category,
			UsageCount: int64(sticker.UsageCount),
		})
	}
	return &chat.GetRandomStickerByCategoryResp{
		Stickers: stickers,
	}, nil
}
